package com.sczmgk.track.base;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.View;

import com.sczmgk.track.callback.OnPermissionSelectedListener;
import com.sczmgk.track.dialog.DialogFactory;
import com.sczmgk.track.dialog.MessageDialog;
import com.sczmgk.track.utils.MyLog;
import com.sczmgk.track.utils.PermissionUtils;
import com.sczmgk.track.utils.Tools;

/**
 * Created by Administrator on 2017/6/12.
 */

public abstract class CheckPermissionBaseActivity extends BaseActivity implements OnPermissionSelectedListener {

    private int type;
    private OnCheckCameraPermissionListerer onCheckCameraPermissionListerer;
    private int position;
    private OncheckLocationPermissionListener oncheckLocationPermissionListener;
    private String phoneNumber;
    protected final int TYPE_CAMERA = 1;
    protected final int TYPE_NORMAL = 2;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case PermissionUtils.REQUEST_PERMISSION_CAMERA:
                MyLog.d(TAG, "request camera  permission result  grantResults.length :" + grantResults.length);
                if (grantResults.length > 0 && grantResults[0] != 0) {
                    //未得到权限,提示用户不开启将不能正常使用
                    MessageDialog messageDialog = DialogFactory.chooseDialog(this, "请选择打开相机权限", "确定", "否", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //调用系统打开权限对话框
//                            ActivityCompat.requestPermissions(SplashActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, Constant.REQUEST_PERMISSION_READ_PHONE_STATE);
                            //跳转至系统设置
                            Uri packageURI = Uri.parse("package:" + CheckPermissionBaseActivity.this.getPackageName());
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                            CheckPermissionBaseActivity.this.startActivity(intent);
                        }
                    }, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MyLog.e(TAG, "use reject camera permission  show dialog");
                            DialogFactory.warningDialog(CheckPermissionBaseActivity.this, "注意", "没有打开相机权限将不能更换头像!", "确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Uri packageURI = Uri.parse("package:" + CheckPermissionBaseActivity.this.getPackageName());
                                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                                    CheckPermissionBaseActivity.this.startActivity(intent);
                                }
                            });
                        }
                    });
                    messageDialog.setTitle("提示");
                    messageDialog.show();
                } else {
                    //已获得权限
                    //打开图片选择，照相，窗口
                    onSelectCamera(position);
                    if(onCheckCameraPermissionListerer != null){
                        onCheckCameraPermissionListerer.onCheckCamera();
                    }
                }
                break;
            case PermissionUtils.REQUEST_PERMISSION_CALL_PHONE:
                MyLog.d(TAG, "request read phone state  permission result  grantResults.length :" + grantResults.length);
                if (grantResults.length > 0 && grantResults[0] != 0) {
                    //未得到权限,提示用户不开启将不能正常使用
                    MessageDialog messageDialog = DialogFactory.chooseDialog(CheckPermissionBaseActivity.this, "请选择打开电话权限", "确定", "否", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //调用系统打开权限对话框
//                            ActivityCompat.requestPermissions(SplashActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, Constant.REQUEST_PERMISSION_READ_PHONE_STATE);
                            //跳转至系统设置
                            Uri packageURI = Uri.parse("package:" + CheckPermissionBaseActivity.this.getPackageName());
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                            CheckPermissionBaseActivity.this.startActivity(intent);
                        }
                    }, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MyLog.e(TAG, "use reject open storage permission  show dialog");
                            DialogFactory.warningDialog(CheckPermissionBaseActivity.this, "注意", "没有打开电话权限将不能正常拨打电话!", "确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Uri packageURI = Uri.parse("package:" + CheckPermissionBaseActivity.this.getPackageName());
                                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                                    CheckPermissionBaseActivity.this.startActivity(intent);
                                }
                            });
                        }
                    });
                    messageDialog.setTitle("提示");
                    messageDialog.show();
                } else {
                    //已获得权限
                    if (!Tools.isEmpty(phoneNumber)) {
                        showCheckDialog();
                    }else {
                        onSelectedPhonePermission(phoneNumber);
                    }
                }
                break;
            case PermissionUtils.REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE:
                MyLog.d(TAG, "request storage  permission result  grantResults.length :" + grantResults.length);

                if (grantResults.length > 0 && grantResults[0] != 0) {
                    //未得到权限,提示用户不开启将不能正常使用
                    MessageDialog messageDialog = DialogFactory.chooseDialog(CheckPermissionBaseActivity.this, "请选择打开写入存储器权限", "确定", "否", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                            ActivityCompat.requestPermissions(SplashActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Constant.REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);
                            Uri packageURI = Uri.parse("package:" + CheckPermissionBaseActivity.this.getPackageName());
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                            CheckPermissionBaseActivity.this.startActivity(intent);
                        }
                    }, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            DialogFactory.warningDialog(CheckPermissionBaseActivity.this, "注意", "没有读取手机状态权限将不能正常使用!", "确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Uri packageURI = Uri.parse("package:" + CheckPermissionBaseActivity.this.getPackageName());
                                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                                    CheckPermissionBaseActivity.this.startActivity(intent);
                                }
                            });
                        }
                    });
                    messageDialog.setTitle("提示");
                    messageDialog.show();
                } else {
                    //已获得权限
                    if (type == TYPE_CAMERA) {
                        checkCamera(position);
                    } else {

                        onSelectedStroage(position);
                    }
                }
                break;
            case PermissionUtils.REQUEST_PERMISSION_RECORD_AUDIO:
                MyLog.d(TAG, "request storage  permission result  grantResults.length :" + grantResults.length);

                if (grantResults.length > 0 && grantResults[0] != 0) {
                    //未得到权限,提示用户不开启将不能正常使用
                    MessageDialog messageDialog = DialogFactory.chooseDialog(CheckPermissionBaseActivity.this, "请选择打开写入存储器权限", "确定", "否", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                            ActivityCompat.requestPermissions(SplashActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, Constant.REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);
                            Uri packageURI = Uri.parse("package:" + CheckPermissionBaseActivity.this.getPackageName());
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                            CheckPermissionBaseActivity.this.startActivity(intent);
                        }
                    }, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            DialogFactory.warningDialog(CheckPermissionBaseActivity.this, "注意", "没有读取手机状态权限将不能正常使用!", "确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Uri packageURI = Uri.parse("package:" + CheckPermissionBaseActivity.this.getPackageName());
                                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                                    CheckPermissionBaseActivity.this.startActivity(intent);
                                }
                            });
                        }
                    });
                    messageDialog.setTitle("提示");
                    messageDialog.show();
                } else {
                    //已获得权限
                    onSelectedRecordAudio();
                }
                break;
            case PermissionUtils.REQUEST_PERMISSION_LOCATION:
                MyLog.d(TAG, "request location  permission result  grantResults.length :" + grantResults.length);
                if (grantResults.length > 0 && grantResults[0] != 0) {
                    //未得到权限,提示用户不开启将不能正常使用
                    MessageDialog messageDialog = DialogFactory.chooseDialog(CheckPermissionBaseActivity.this, "请选择打开定位权限", "确定", "否", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //调用系统打开权限对话框
//                            ActivityCompat.requestPermissions(SplashActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, Constant.REQUEST_PERMISSION_READ_PHONE_STATE);
                            //跳转至系统设置
                            Uri packageURI = Uri.parse("package:" + CheckPermissionBaseActivity.this.getPackageName());
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                            CheckPermissionBaseActivity.this.startActivity(intent);
                        }
                    }, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            MyLog.e(TAG, "use reject open location permission  show dialog");
                            DialogFactory.warningDialog(CheckPermissionBaseActivity.this, "注意", "没有打开定位权限将不能正确定位到你的区域!", "确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Uri packageURI = Uri.parse("package:" + CheckPermissionBaseActivity.this.getPackageName());
                                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                                    CheckPermissionBaseActivity.this.startActivity(intent);
                                }
                            });
                        }
                    });
                    messageDialog.setTitle("提示");
                    messageDialog.show();
                } else {
                    //已获得权限
                    checkIsOpenLocation(true);
                }
                break;
            case PermissionUtils.REQUEST_LOCATION_SOURCE_SETTINGS:
                //打开用户位置设置的回调
                //1.检查是否打开，如打开则在回调用中调用定位
                //2.未打开，则不做任何操作
                checkIsOpenLocation(false);
                break;
        }
    }

    private void showCheckDialog() {
        MessageDialog messageDialog = DialogFactory.chooseDialog(CheckPermissionBaseActivity.this, "是否拨打电话", "确定", "否", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onSelectedPhonePermission(phoneNumber);
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        messageDialog.show();
    }


    /**
     * 检查电话权限
     *
     * @param phoeNumber
     */
    protected void checkPhone(String phoeNumber) {
        MyLog.d(TAG, "check phone ");
        this.phoneNumber = phoeNumber;
        boolean checkPhonePermission = PermissionUtils.checkPermission(CheckPermissionBaseActivity.this, Manifest.permission.CALL_PHONE);//电话权限
        if (!checkPhonePermission) {
            MyLog.e(TAG, "no phone state permission ,requestPermission");
            ActivityCompat.requestPermissions(CheckPermissionBaseActivity.this, new String[]{Manifest.permission.CALL_PHONE}, PermissionUtils.REQUEST_PERMISSION_CALL_PHONE);
        } else {
            //TODO 打电话
            if (!Tools.isEmpty(phoeNumber)) {
                showCheckDialog();
            }else {
                onSelectedPhonePermission(phoeNumber);
            }
        }
    }


    /**
     * 检查MIC 权限
     */
    protected void checkRecordAudio() {
        MyLog.d(TAG, "check  record audio");

        boolean b = PermissionUtils.checkPermission(CheckPermissionBaseActivity.this, Manifest.permission.RECORD_AUDIO);
        if (!b) {
            ActivityCompat.requestPermissions(CheckPermissionBaseActivity.this, new String[]{android.Manifest.permission.RECORD_AUDIO}, PermissionUtils.REQUEST_PERMISSION_RECORD_AUDIO);
        } else {
            onSelectedRecordAudio();
        }

    }


    /**
     * 检查相机权限
     *
     * @param position 多张图片上传时传入点击条目的position
     */
    protected void checkCamera(int position) {
        MyLog.d(TAG, "check camera");
        this.position = position;
        //先检查存储权限
        checkStroage(position, TYPE_CAMERA);
    }    /**
     * 检查相机权限
     *
     * @param position 多张图片上传时传入点击条目的position
     */
    public void checkCamera(int position,OnCheckCameraPermissionListerer onCheckCameraPermissionListerer) {
        MyLog.d(TAG, "check camera");
        this.onCheckCameraPermissionListerer = onCheckCameraPermissionListerer;
        this.position = position;
        //先检查存储权限
        checkStroage(position, TYPE_CAMERA);


    }

    /**
     * 检查存储权限
     *
     * @param position 仅检查存储权限时传入任意 数值
     * @param type     仅检查存储仅限里传入  TYPE_NORAM
     */
    protected void checkStroage(int position, int type) {
        MyLog.i(TAG, "check storage");
        this.type = type;
        this.position = position;
        boolean checkPermission = PermissionUtils.checkPermission(CheckPermissionBaseActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (!checkPermission) {
            //没有打开存储
            ActivityCompat.requestPermissions(CheckPermissionBaseActivity.this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, PermissionUtils.REQUEST_PERMISSION_WRITE_EXTERNAL_STORAGE);
        } else {
            if (type == TYPE_NORMAL) {
                onSelectedStroage(position);
            } else {
                boolean checkPermission1 = PermissionUtils.checkPermission(CheckPermissionBaseActivity.this, Manifest.permission.CAMERA);
                if (!checkPermission1) {
                    //没有打开权限
                    ActivityCompat.requestPermissions(CheckPermissionBaseActivity.this, new String[]{android.Manifest.permission.CAMERA}, PermissionUtils.REQUEST_PERMISSION_CAMERA);
                } else {
                    onSelectCamera(position);
                    if(onCheckCameraPermissionListerer != null){
                        onCheckCameraPermissionListerer.onCheckCamera();
                    }
                }
            }
        }
    }

    @Override
    public void onSelectedRecordAudio() {

    }

    @Override
    public void onSelectLocation() {

    }

    /**
     * 检查定位权限
     */
    protected void checkLocation() {
        boolean permission = PermissionUtils.checkPermission(CheckPermissionBaseActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
        if (!permission) {
            //请求打开位置权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PermissionUtils.REQUEST_PERMISSION_LOCATION);
        } else {
            //检查是否打开定位
            checkIsOpenLocation(true);
        }
    }

    /**
     * 检查定位权限
     */
    public void checkLocation(OncheckLocationPermissionListener oncheckLocationPermissionListener) {
        this.oncheckLocationPermissionListener = oncheckLocationPermissionListener;

        boolean permission = PermissionUtils.checkPermission(CheckPermissionBaseActivity.this, Manifest.permission.ACCESS_FINE_LOCATION);
        if (!permission) {
            //请求打开位置权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PermissionUtils.REQUEST_PERMISSION_LOCATION);
        } else {
            //检查是否打开定位
            checkIsOpenLocation(true);
        }
    }


    /**
     * 检查是否打开定位
     *
     * @param isFirstCheck   是否为第一次检查
     */
    private void checkIsOpenLocation(boolean isFirstCheck) {
        boolean locationStatus = PermissionUtils.checkLocationStatus(this);
        //第一次检查则弹出对话框  1. 已打开权限  2.提示打开权限后去打开了权限
        //已弹出对话框，用户不愿意打开定位

            if (!locationStatus) {
                if(isFirstCheck){
                    PermissionUtils.showOpenLocationDialog(this);
                }
            }else{
                if(oncheckLocationPermissionListener != null)
                    oncheckLocationPermissionListener.onCheckLocation();
            }

    }
    public interface OncheckLocationPermissionListener{
       void  onCheckLocation();
    }

    public interface OnCheckCameraPermissionListerer{
        void onCheckCamera();
    }
}

